<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Cache;

class Index extends Controller
{
    public function index(){

		return $this->fetch('index/index2');
	}
    public function self(){
        $user = Db::name('users')->where('id',cookie('uid'))->find();
        $this->assign('s',$user);
		return $this->fetch('index/self');
	}
    public function member(){
        if(!cookie('uid')){
            $this->redirect('index/index');
        }else{
            $user = Db::name('users')->where('id',cookie('uid'))->find();
            if($user['password']!=cookie('pwd')){
                $this->redirect('index/member');
            }
        }
        $info = Db::name('info')->where('uid',cookie('uid'))->select();
        $this->assign('info',$info);
		return $this->fetch('index/member');
	}
    public function login()
    {
        if(input('post.')){
            $mobile = input('mobile');
            $pwd = input('password');
            $u = Db::name('users')->where('mobile',$mobile)->find();
            $pwd = md5($pwd.$u['salt']);
            if($u['password'] == $pwd){
                cookie('uid',$u['id']);
                cookie('pwd',$u['password']);
                $this->redirect('member');
            }else{
                $this->error('密码错误！！！');

            }
        }else{
            return $this->fetch();
        }
    }
    public function fabu()
    {
        if(input('post.')){
            $area = Db::name('area')->select();
            $a = array_rand($area,30);
            foreach ($a as $v){
                $res['areaid'] = $area[$v]['id'];
                $data = input('post.');
                $title[] = $data['title1'];
                $title[] = $data['title2'];
                $title[] = $data['title3'];
                $title[] = $data['title4'];
                $title[] = $data['title5'];
                $title[] = $data['title6'];
                $t = array_rand($title,3);
                $res['title'] = $data['title7'].$title[$t[0]].$title[$t[1]].$title[$t[2]].$data['title8'];
                $key[] = $data['keywords1'];
                $key[] = $data['keywords2'];
                $key[] = $data['keywords3'];
                $key[] = $data['keywords4'];
                $key[] = $data['keywords5'];
                $key[] = $data['keywords6'];
                $k = array_rand($key,2);
                $res['keywords1'] = $key[$k[0]];
                $res['keywords2'] = $key[$k[1]];
                $content[] = $data['content1'];
                $content[] = $data['content2'];
                $content[] = $data['content3'];
                $content[] = $data['content4'];
                $content[] = $data['content5'];
                $content[] = $data['content6'];

                $c = array_rand($content,3);
                $res['content1'] = $content[$c[0]];
                $res['content2'] = $content[$c[1]];
                $res['content3'] = $content[$c[2]];
                $res['description'] = $res['title'].$res['content1'];
                $cat = [1,2,3,4,5,6];
                $res['catid'] = $cat[array_rand($cat,1)];
                $res['uid'] = cookie('uid');
                $res['time'] = time();

                Db::name('info')->insert($res);
            }
            $this->success('发布成功！');

        }else{
            return $this->fetch();
        }
    }

	public function job(){
		$info = Db::name('info')->where('catid',1)->find();
		$this->assign('h',$info);
		return $this->fetch('index/index');
	}

	public function info(){
		$info = Db::name('info')->where('id',input('id'))->find();
		$this->assign('h',$info);

        $ding = Db::name('info')->order('ding desc')->limit(20)->select();
		$this->assign('h',$info);
		$this->assign('ding',$ding);
		return $this->fetch();
	}


	public function header(){
        $area = '北京';
        $this->assign('area',$area);
        return $this->fetch();
    }

    public function message(){
        if(input('post.')) {
            $mobile = input('mobile');
            $uid =  Db::name('info')->where('id',input('id'))->value('uid');
            Db::name('message')->insert([
                'uid'=>$uid,
                'mobile'=>$mobile,
            ]);
            $this->success('提交成功');
        }else{
            $message =  Db::name('message')->where('uid',cookie('uid'))->order('id desc')->select();
            $this->assign('message',$message);
            return $this->fetch();
        }
    }
}